// pages/visits/visits.js    hyb_yl/zhuanjiasubpages/pages/zhifuend/zhifuend
var app = getApp()
var Utils = require('../../../../utils/util.js');
const generatetestusersig = require("../../../webrtc-room/debug/generatetestusersig.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    types: "",
    tablistwz: ["全部","待支付", "待接诊", "已接诊", "已结束", "退款", "已取消"],
    tablistss: ["全部","待支付", "待接诊", "已接诊", "已结束", "退款", "已取消"],
    currentData: 0,
    ifpay: 0,
    accomplish: []
  },
  //获取当前滑块的index
  bindchange: function (e) {
    const that = this;
    that.setData({
      currentData: e.detail.current
    })
  },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    console.log(e)
    const that = this;
    var ifpay = e.target.dataset.current
    var key_words = that.data.key_words
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    if (key_words == 'tuwenwenzhen') {
      console.log('222')
      app.util.request({
        'url': 'entry/wxapp/tuwen.oderstate',
        'data': {
          openid: wx.getStorageSync('openid'),
          ifpay: ifpay,
          key_words: key_words
        },
        success: function (res) {
          console.log(res)
          that.setData({
            tuwenwz: res.data,
            ifpay: ifpay,
            currentData: ifpay
          })
        },
      })
    }
    if (key_words == 'dianhuajizhen' || key_words == 'shipinwenzhen' || key_words == 'shoushukuaiyue') {
      app.util.request({
        'url': 'entry/wxapp/yuyue.oderstate',
        'data': {
          openid: wx.getStorageSync('openid'),
          ifpay: ifpay,
          key_words: key_words
        },
        success: function (res) {
          console.log(res)
          that.setData({
            lilst: res.data
          })
        },
      })
    }
    if (key_words == 'yuanchengkaifang') {
      //关闭超时订单

      app.util.request({
        'url': 'entry/wxapp/yuyue.odercf',
        'data': {
          openid: wx.getStorageSync('openid'),
          ispay: ifpay,
          key_words: key_words,
          timestamp: timestamp
        },
        success: function (res) {
          console.log(res)
          that.setData({
            lilst: res.data,
          })
        },
      })
    }
    that.setData({
      currentData: e.target.dataset.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.channel = "";
    this.uid = Utils.getUid();
    this.lock = false;
    var key_words = options.key_words
    let userInfo = wx.getStorageSync("userInfo");
    if (userInfo) {
      this.setData({
        hasUserInfo: true,
        userInfo: userInfo,
        u_name: userInfo.u_name,
        u_thumb: userInfo.u_thumb,
      });
    }
    var that = this
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.setData({
      backgroundColor: bgc,
      types: options.type,
      key_words: key_words
    })
    that.getalltuwenwz()
    //获取订阅消息id
    app.util.request({
      'url': 'entry/wxapp/index.techen',
      success: function (res) {
        console.log(res)
        var msg = res.data.wxapp_mb
        that.setData({
          submitOrder: msg.submitOrder, //就诊提醒
          OpenHalfcard: msg.OpenHalfcard, //用药提醒提醒
          Mobile: msg.Mobile, //问诊提醒（提醒患者，提醒医生通用）
          hexiao: msg.hexiao, //问诊状态提醒退款不接诊等等
          spstratSuccess: msg.spstratSuccess //接种提醒
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var key_words = that.data.key_words
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    var ifpay = currPage.data.currentData
    if (key_words == 'tuwenwenzhen') {
      app.util.request({
        'url': 'entry/wxapp/tuwen.oderstate',
        'data': {
          openid: wx.getStorageSync('openid'),
          ifpay: ifpay,
          key_words: key_words
        },
        success: function (res) {
          console.log(res)
          that.setData({
            tuwenwz: res.data,
            ifpay: ifpay,
            currentData: ifpay
          })
        },
      })
    }
    if (key_words == 'dianhuajizhen' || key_words == 'shipinwenzhen' || key_words == 'shoushukuaiyue') {
      app.util.request({
        'url': 'entry/wxapp/yuyue.oderstate',
        'data': {
          openid: wx.getStorageSync('openid'),
          ifpay: ifpay,
          key_words: key_words
        },
        success: function (res) {
          //console.log(res)
          that.setData({
            lilst: res.data
          })
        },
      })
    }
    if (key_words == 'yuanchengkaifang') {
      app.util.request({
        'url': 'entry/wxapp/yuyue.odercf',
        'data': {
          openid: wx.getStorageSync('openid'),
          ispay: ifpay,
          key_words: key_words
        },
        success: function (res) {
          console.log(res)
          that.setData({
            lilst: res.data
          })
        },
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getalltuwenwz() {
    var that = this
    var ifpay = that.data.ifpay
    var key_words = that.data.key_words
    if (key_words == 'tuwenwenzhen') {
      app.util.request({
        'url': 'entry/wxapp/tuwen.oderstate',
        'data': {
          openid: wx.getStorageSync('openid'),
          ifpay: ifpay,
          key_words: key_words
        },
        success: function (res) {
          console.log(res)
          that.setData({
            tuwenwz: res.data
          })
        },
      })
    }
    //电话问诊
    if (key_words == 'dianhuajizhen' || key_words == 'shipinwenzhen' || key_words == 'shoushukuaiyue') {
      app.util.request({
        'url': 'entry/wxapp/yuyue.oderstate',
        'data': {
          openid: wx.getStorageSync('openid'),
          ifpay: ifpay,
          key_words: key_words
        },
        success: function (res) {
          console.log(res)
          that.setData({
            lilst: res.data
          })
        },
      })
    }
    if (key_words == 'yuanchengkaifang') {
      app.util.request({
        'url': 'entry/wxapp/yuyue.odercf',
        'data': {
          openid: wx.getStorageSync('openid'),
          ispay: ifpay,
          key_words: key_words
        },
        success: function (res) {
          console.log(res)
          that.setData({
            lilst: res.data
          })
        },
      })
    }
    if (key_words == 'shoushukuaiyue') {
      app.util.request({
        'url': 'entry/wxapp/user.allshoushuorder',
        data: {
          openid: wx.getStorageSync('openid'),
          index: ifpay
        },
        success: function (res) {
          console.log(res)
          that.setData({
            lilst: res.data
          })
        }
      })
    }
  },
  zixunxq(e) {
    var that = this
    console.log(e)
    var list = e.currentTarget.dataset
    var orders = e.currentTarget.dataset.orders
    var j_id = e.currentTarget.dataset.j_id
    var key_words = that.data.key_words
    console.log(key_words)
    var zid = e.currentTarget.dataset.zid
    var ifpay = e.currentTarget.dataset.ifpay
    var money = e.currentTarget.dataset.money
    var overtime = e.currentTarget.dataset.overtime
    var currentData = that.data.currentData
    var ifgb = e.currentTarget.dataset.ifgb
    wx.setStorage({
      key: 'userinfotype',
      data: 'user',
    })
    let userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo)
    let bl_id = e.currentTarget.dataset.bl_id
    wx.setStorage({
      key: 'userInfo',
      data: userInfo,
    })
    let z_thumbs = e.currentTarget.dataset.z_thumbs //医生头像
    wx.setStorage({
      key: 'z_thumbs',
      data: z_thumbs,
    })
    let sate = 0
    wx.setStorageSync('sate', sate) //证明是用户发送的信息
    wx.setStorageSync('docroom', your)
    var my = e.currentTarget.dataset.myroom; //自己
    var your = e.currentTarget.dataset.docroom; //医生
    var dex = e.currentTarget.dataset.dex;
    wx.setStorageSync('docroom', your)
    var nameList = {
      myName: my,
      your: your,
      bl_id: bl_id
    };
    console.log(e)
    var that = this
    var spstratSuccess = that.data.spstratSuccess
    var OpenHalfcard = that.data.OpenHalfcard
    var Mobile = that.data.Mobile
    var hexiao = that.data.hexiao
    var settled = that.data.settled
    wx.requestSubscribeMessage({
      tmplIds: [Mobile, OpenHalfcard],
      success: (res) => {
        console.log(res)
        //调用授权
      }
    })
    if (ifpay == 5) {
      wx.showToast({
        title: '退款申请中无法查看详情',
        icon: 'none'
      })
      return false;
    }
    if (ifpay == 6) {
      wx.showToast({
        title: '已退款',
        icon: 'none'
      })
      return false;
    }
    if (key_words == 'tuwenwenzhen') {
      wx.navigateTo({
        url: '/hyb_yl/czhuanjiasubpages/pages/chatroom/chatroom?username=' + JSON.stringify(nameList) + '&list=' + JSON.stringify(list) + '&z_name=' + e.currentTarget.dataset.z_name + '&orders=' + orders + '&dex=' + dex + '&key_words=' + key_words + '&ifpay=' + ifpay + '&money=' + money + '&overtime=' + overtime + '&currentData=' + currentData + '&ifgb=' + ifgb + '&txt=yes',
      })
    }
    if (key_words == 'dianhuajizhen' || key_words == 'shipinwenzhen' || key_words == 'shoushukuaiyue') {
      wx.navigateTo({
        url: '/hyb_yl/zhuanjiasubpages/pages/zhifuend/zhifuend?txt=yes&zid=' + zid + '&back_orser=' + orders + '&key_words=' + key_words + '&j_id=' + j_id + '&ifpay=' + ifpay + '&money=' + money + '&overtime=' + overtime + '&currentData=' + currentData + '&ifgb=' + ifgb,
      })
    }
    if (key_words == "yuanchengkaifang") {
      var c_id = e.currentTarget.dataset.c_id
      wx.navigateTo({
        url: '/hyb_yl/zhuanjiasubpages/pages/zhifuend/zhifuend?txt=yes&zid=' + zid + '&back_orser=' + orders + '&key_words=' + key_words + '&j_id=' + j_id + '&ifpay=' + ifpay + '&money=' + money + '&overtime=' + overtime + '&currentData=' + currentData + '&ifgb=' + ifgb + '&c_id=' + c_id,
      })
    }
    if (key_words == "shoushukuaiyue") {
      wx.navigateTo({
        url: '/hyb_yl/zhuanjiasubpages/pages/zhifuend/zhifuend?txt=yes&zid=' + zid + '&back_orser=' + orders + '&key_words=' + key_words + '&j_id=' + j_id + '&ifpay=' + ifpay + '&money=' + money + '&overtime=' + overtime + '&currentData=' + currentData + '&ifgb=' + ifgb,
      })
    }
  },
  pingjia(e) {
    console.log(e)
    var that = this
    var keywords = e.currentTarget.dataset.keywords
    var zid = e.currentTarget.dataset.zid
    var orders = e.currentTarget.dataset.orders
    var j_id = e.currentTarget.dataset.j_id
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/pingjia/pingjia?keywords=' + keywords + '&zid=' + zid + '&orders=' + orders + '&j_id=' + j_id,
    })
  },
  gettel(e) {
    var that = this
    var ifpay = e.currentTarget.dataset.ifpay
    if (ifpay == '1') {
      wx.showToast({
        title: '待接诊',
        icon: 'none'
      })
    }
  },
  joinRoom: function (e) {
    console.log(e)
    var self = this;
    var roomID = '123456'
    self.data.userID = new Date().getTime().toString(16);
    var userSig = generatetestusersig.gentestusersig(self.data.userID);
    console.log(userSig, self.data.userID)
    // 防止两次点击操作间隔太快
    var ifpay = e.currentTarget.dataset.ifpay
    if (ifpay == '1') {
      wx.showToast({
        title: '请等待医生接诊',
        icon: 'none'
      })
      return false;
    }
    var nowTime = new Date();
    if (nowTime - this.data.tapTime < 1000) {
      return;
    }
    self.data.userID = new Date().getTime().toString(16);
    var userSig = generatetestusersig.gentestusersig(self.data.userID);
    var url = `../../../webrtc-room/room/room?roomID=${roomID}&template=1v1&sdkAppID=${userSig.sdkAppID}&userId=${self.data.userID}&userSig=${userSig.userSig}`;
    console.log(url)
    wx.navigateTo({
      url: url
    });
    wx.showToast({
      title: '进入房间',
      icon: 'success',
      duration: 1000
    })
    self.setData({
      'tapTime': nowTime
    });
  },

})